﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/3/26 星期二 19:33:19
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.EntityType;
using Sector7.Framework.DB;
using Sector7.Framework.Consts;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;

namespace Sector7.Framework.DAO.Standard
{
    /// <summary>
    /// 数据元模板数据访问对象
    /// </summary>
    public class DataTemplateDAO : DAOBase<S7_DataTemplate>, IDataTemplateDAO
    {
        #region Public functions
        public override object Create(S7_DataTemplate dataTemplate)
        {
            string sql = @"
                INSERT INTO S7_DataTemplate
                        (TemplateName
                        ,TableName
                        ,GroupId)
                    VALUES
                        (@TemplateName
                        ,@TableName
                        ,@GroupId)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(dataTemplate.TemplateName, "TemplateName"),
                    DBUtil.CreateSqlParameter(dataTemplate.TableName, "TableName"),
                    DBUtil.CreateSqlParameter(dataTemplate.GroupId, "GroupId"));
            }
        }

        public override List<S7_DataTemplate> ReadAll()
        {
            string sql = @"
                SELECT *
                    FROM S7_DataTemplate";

            List<S7_DataTemplate> templates = new List<S7_DataTemplate>();

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql);

                while (reader.Read())
                {
                    var template = new S7_DataTemplate();
                    DBUtil.ReadData(reader, template);

                    IDataTemplateItemDAO dao = new DataTemplateItemDAO();
                    template.Items = dao.ReadAll(template.TemplateId);

                    templates.Add(template);
                }
            }

            return templates;
        }

        public List<S7_DataTemplate> ReadAll(int groupId)
        {
            string sql = @"
                SELECT *
                    FROM S7_DataTemplate
                WHERE
                    GroupId = @GroupId";

            List<S7_DataTemplate> templates = new List<S7_DataTemplate>();

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(groupId, "GroupId"));

                while (reader.Read())
                {
                    var template = new S7_DataTemplate();
                    DBUtil.ReadData(reader, template);

                    IDataTemplateItemDAO dao = new DataTemplateItemDAO();
                    template.Items = dao.ReadAll(template.TemplateId);

                    templates.Add(template);
                }
            }

            return templates;
        }
        #endregion
    }
}
